package controllers

import (
	//"fmt"
	"github.com/astaxie/beego"
	"jiabo/appframework/libs/accountandrole"
	"jiabo/appframework/libs/common"
)

type LoginController struct {
	beego.Controller
}

func (this *LoginController) Get() {
	this.Ctx.Request.ParseForm()
	// todo := this.Ctx.Request.Form.Get("todo")
	// if todo == "logout" {
	// 	sess := this.StartSession()
	// 	sess.Delete("username")
	// }

	this.TplNames = "login.html"
}

func (this *LoginController) Post() {
	username := this.GetString("username")
	password := this.GetString("password")

	account, err := accountandrole.GetAccountByUserName(username)
	if err == nil {
		if account.Password == common.MD5(password) {
			sess := this.StartSession()
			sess.Set("username", username)
			sess.Set("fullname", account.Fullname)
			this.Ctx.Redirect(302, "/")
		} else {
			this.Ctx.Redirect(302, "/message?msg=loginerr")
		}
	} else {
		this.Ctx.Redirect(302, "/message?msg="+err.Error())
	}
}
